/**
 * Copyright (C), 2020-2020,贵州铭明网络科技有限公司
 * FileName: AdministratorInfoController
 * Author:   杨朝湖
 * Date:     2020/12/8 11:05
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.mm.controller;

import com.mm.bo.AdminListBo;
import com.mm.bo.AdminLoginBo;
import com.mm.bo.AdminUpateBo;
import com.mm.bo.InsertAdminInfoBo;
import com.mm.exception.CustomException;
import com.mm.service.AdministratorInfoService;
import com.mm.util.GetHeadUtil;
import com.mm.util.MD5Util;
import com.mm.util.PageUtil;
import com.mm.util.ResultUtil;
import com.mm.vo.AdminListVo;
import com.mm.vo.AdminLoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Gym
 * @create 2020/12/8
 * @since 1.0.0
 */
@Api(value = "管理员", tags = "用于管理员的相关接口")
@RestController
public class AdministratorInfoController {
    @Resource
    private AdministratorInfoService administratorInfoService;

    @ApiOperation(value = "添加管理员信息", notes = "添加管理员", httpMethod = "POST")
    @PostMapping("/v1/admin/insertAdminInfo")
    public ResultUtil<Object> insertAdminInfo(@RequestBody @Valid InsertAdminInfoBo insertAdminInfoBo) throws Exception {
        insertAdminInfoBo.setToken(GetHeadUtil.getHeadToken());
        Integer row = administratorInfoService.insertAdminInfo(insertAdminInfoBo);
        if (row < 1) {
            throw new CustomException("添加管理员信息失败");
        }
        return ResultUtil.success(null);
    }

    @ApiOperation(value = "管理员登录", notes = "管理员登录", httpMethod = "POST")
    @PostMapping("/v1/admin/login")
    public ResultUtil<Object> login(@RequestBody @Valid AdminLoginBo adminLoginBo) throws Exception {
        AdminLoginVo adminLoginVo = administratorInfoService.login(adminLoginBo);
        return ResultUtil.success(adminLoginVo);
    }

    @ApiOperation(value = "查询管理员列表", notes = "管理员列表", httpMethod = "POST")
    @PostMapping("/v1/admin/queryAdminList")
    public ResultUtil<Object> queryAdminList(@RequestBody @Valid AdminListBo adminListBo) {
        adminListBo.setToken(GetHeadUtil.getHeadToken());
        PageUtil<AdminListVo> adminListVoPageUtil = administratorInfoService.queryAdminList(adminListBo);
        return ResultUtil.success(adminListVoPageUtil);
    }

    @ApiOperation(value = "修改管理员信息", notes = "修改管理员", httpMethod = "POST")
    @PostMapping("/v1/admin/updateAdminInfo")
    public ResultUtil<Object> updateAdminInfo(@RequestBody @Valid AdminUpateBo adminUpateBo) throws Exception {
        adminUpateBo.setToken(GetHeadUtil.getHeadToken());
        Integer row = administratorInfoService.updateAdminInfo(adminUpateBo);
        if (row < 1) {
            throw new CustomException("修改失败");
        }
        return ResultUtil.success(null);
    }

    @ApiOperation(value = "重置管理员密码", notes = "重置管理员密码", httpMethod = "GET")
    @GetMapping("/v1/admin/resetAdminPassword/{adminId}")
    public ResultUtil<Object> resetAdminPassword(@PathVariable("adminId") String adminId) {
        Integer row = administratorInfoService.resetAdminPassword(adminId, GetHeadUtil.getHeadToken());
        if (row < 1) {
            throw new CustomException("重置失败");
        }
        return ResultUtil.success(null);
    }

    @ApiOperation(value = "删除管理员信息", notes = "删除管理员信息", httpMethod = "GET")
    @GetMapping("/v1/admin/deleteAdminInfo/{adminId}")
    public ResultUtil<Object> deleteAdminInfo(@PathVariable("adminId") String adminId) {
        Integer row = administratorInfoService.deleteAdminInfo(adminId, GetHeadUtil.getHeadToken());
        if (row < 1) {
            throw new CustomException("重置失败");
        }
        return ResultUtil.success(null);
    }

    @ApiOperation(value = "锁定管理员信息", notes = "锁定管理员信息", httpMethod = "GET")
    @GetMapping("/v1/admin/lockingAdminInfo/{adminId}")
    public ResultUtil<Object> lockingAdminInfo(@PathVariable("adminId") String adminId) {
        Integer row = administratorInfoService.lockingAdminInfo(adminId, GetHeadUtil.getHeadToken());
        if (row < 1) {
            throw new CustomException("锁定失败");
        }
        return ResultUtil.success(null);
    }
}